/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-10-07 17:36:54
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-10-07 17:52:43
 * @FilePath: /sjf/2024-project/2024_centos/test_to_c/stack_and_queue/queue_test.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "queue_test.h"

void queue_init(queue* q)
{
    q->_head = q->_tail = NULL;
}

void queue_destory(queue* q)
{
    node* cur = q->_head;
    while(cur)
    {
        node* next = cur->_next;
        free(cur);
        cur = next;
    }
    q->_head = NULL;
    q->_tail = NULL;
}

void queue_push(queue* q,QDataType x)
{
    node* newnode = (QDataType*)malloc(sizeof(QDataType));
    newnode->_data = x;
    newnode->_next = NULL;
    if(q->_head == NULL)
        q->_head = q->_tail = newnode;
    else
    {
        q->_tail->_next = newnode;//将最后一个节点指向新增的节点
        q->_tail = newnode;//然后让位指针指向最新的节点newnode
    }
}

void queue_pop(queue* q)
{
    node* cur = q->_head->_next;
    if(q->_head->_next == NULL)
    {
        free(q->_head);
        q->_head = NULL;
        q->_tail = NULL;
    }
    else
    {
        free(q->_head);
        q->_head = cur;
    }
}

QDataType get_front(queue* q)
{
    assert(q);
    assert(!queue_empty(q));
    return q->_head->_data;
}

QDataType get_back(queue* q)
{
    assert(q);
    assert(!queue_empty(q));
    return q->_tail->_data;
}

bool queue_empty(queue* q)
{
    return q->_head = NULL;
}

int queue_size(queue* q)
{
    node* cur = q->_head;
    int count = 0;
    while(cur)
    {
        count++;
        cur = cur->_next;
    }
    return count;
}